<template>
  <div class="resumeItemContainer">
    <div class="resumeItemTitle">
      <div class="resumeItemTitleFlag"></div>
      <span class="resumeItemTitleContent">教育背景</span>
      <span class="resumeItemTitleAddIcon" @click="handleAddClick"><img :src="resumeItemAddIcon"
          class="resumeItemAddIcon"> 添加</span>
    </div>
    <div class="resumeItemMain">
      <div class="resumeItemDisplayList">
        <div class="resumeItemDisplayItem" v-for="(item, index) in resumeDataList" :key="index">
          <div class="resumeItemDisplayItemEditButtons">
            <template v-if="!item.editStatus">
              <span class="resumeItemDisplayItemEditButton" @click="changeEditStatus(item, true)">
                <img :src="myResumeEditIcon" /> 编辑</span>
              <span class="resumeItemDisplayItemEditButton" @click="handleDeleteClick(item)">
                <img :src="myResumeDeleteIcon" />删除</span>
            </template>
          </div>
          <div class="displayBlock" v-if="!item.editStatus">
            <!-- 非编辑状态 -->
            <div class="displayInfo">
              <span class="bold"> {{ item.school }} </span>
              <span class="divide" v-if="item.startdatev2 && item.stopdatev2"></span>
              <span class="nobold">
                {{ item.startdatev2 }} - {{ item.stopdatev2 }}</span>
              <span class="divide" v-if="item.eduId"></span>
              <span> {{ handleEduId(item.eduId) }}</span>
              <span class="divide" v-if="item.zye"></span>
              <span> {{ item.zye }}</span>
            </div>
            <div class="summaryContainer">
              <div class="summary richContent" :ref="`richContent${index}`" v-html="item.edus"></div>
              <div class="expendContainer" @click="expandContent(item,index)" v-if="item.showExpend">
                <el-button type="text" :icon="item.expendButtonText=='展开'?'el-icon-arrow-down':'el-icon-arrow-up'">
                  {{item.expendButtonText||"展开"}}
                </el-button>
              </div>
            </div>
          </div>
          <div class=" editBlock" v-else :id="item.editStatus?'edittingBlock':''">
            <div class="resumeItemEdit">
              <el-form :ref="'ruleForm' + item.edusId" :rules="rules" :model="item.editForm" inline>
                <el-form-item label="学校名称:" prop="school">
                  <el-input style="width: 250px" v-model="item.editForm.school" placeholder="请输入学校名称"
                    maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="学历:" prop="eduId">
                  <el-select v-model="item.editForm.eduId" placeholder="请选择学历" @change="handleEduChange(item)">
                    <el-option style="width: 250px" v-for="item in eduList" :key="item.value" :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="专业名称:" prop="zyeArr">
                  <el-cascader style="width: 250px" ref="educationInfo_zyeArr_cascader"
                    :visible="cascaderVisibleStates.educationInfo_zyeArr_cascader" v-model="item.editForm.zyeArr"
                    filterable :options="majorList" :props="{ expandTrigger: 'hover' }" @change="handleMajorChange"
                    :style="{ width: '100%' }" placeholder="请选择专业名称" />
                </el-form-item>
                <el-form-item label="就读时间:" prop="startdate">
                  <el-date-picker style="width: 250px; height: 40px" v-model="item.editForm.startdate" size="small"
                    type="monthrange" align="right" unlink-panels value-format="yyyy-MM-dd" range-separator="至"
                    start-placeholder="开始时间" end-placeholder="结束时间">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="教育背景描述:" prop="edus" class="textAreaFormItem">
                  <el-input type="textarea" :rows="10" :maxlength="1600" show-word-limit placeholder="请输入教育背景"
                    v-model="item.editForm.edus">
                  </el-input>
                </el-form-item>
                <el-form-item class="wholeLineFormItem">
                  <div class="submitButtons">
                    <el-button size="small" @click="handleCancelClick(item)">取 消</el-button>
                    <el-button size="small" type="primary" @click="handleSave(item)">完 成</el-button>
                  </div>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import majorList from "@/utils/majorList";
import { eduList } from "@/utils/resumeData";
import myResumeDeleteIcon from "@/assets/images/png/myResumeDeleteIcon.png";
import myResumeEditIcon from "@/assets/images/png/myResumeEditIcon.png";
import myResumeItemsMixin from "@/mixin/myResumeItemsMixin.js";
export default {
  name: "educationInfo",
  mixins: [myResumeItemsMixin],
  props: {
    resumeData: {
      type: Array,
      default: () => [],
    },
    editStatus: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      majorList,
      eduList,
      myResumeEditIcon,
      myResumeDeleteIcon,
      resumeDataList: [], //
      zyeArr: [],
      // startdate: [],
      // regionCascaderProps: {
      //   checkStrictly: true, // 严格检查，不自动勾选父级
      //   value: "value",
      //   label: "label",
      //   children: "children",
      //   emitPath: false, // 不返回完整的路径，只返回最终选中的值
      // },
      rules: {
        school: [
          { required: true, message: "请输入学校名称", trigger: "blur" },
          {
            min: 2,
            max: 100,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        eduId: [{ required: true, message: "请选择学历", trigger: "change" }],
        zyeArr: [
          { required: true, message: "请选择专业名称", trigger: "change" },
        ],
        startdate: [
          { required: true, message: "请选择就读时间", trigger: "change" },
        ],
        edus: [
          { required: true, message: "请输入教育背景描述", trigger: "blur" },
          {
            min: 2,
            max: 1600,
            message: "长度在 2 到 1600 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    resumeData: {
      immediate: true,
      handler(newValue, oldValue) {
        //console.log("教育背景educationInfo基本信息更新：", newValue);
        if (!newValue) return;
        this.resumeDataList = newValue.map((item, index) => {
          if (!item.hasOwnProperty("editStatus")) {
            this.$set(item, "editStatus", false);
          }
          if (!item.hasOwnProperty("editForm")) {
            this.$set(item, "editForm", { ...item });
            item.editForm.zyeArr = [item.zym, item.zye];
            item.editForm.startdate = [item.startdate, item.stopdate];
          }
          if (!item.hasOwnProperty("showExpend")) {
            this.isOverflowing(item, `richContent${index}`);
          }
          return item;
        });
      },
    },
    editStatus: {
      handler(newValue, oldValue) {
        //如果新值是false要把所有的editStatus改为false
        if (newValue == false) {
          this.resumeDataList.forEach((item) => {
            item.editStatus = false;
            if (item.isNew) {
              this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1); // 新的删除
            }
          });
        }
      },
    },
  },
  methods: {
    handleMajorChange(value) {
      //console.log("----------->handleMajorChange", value);
    },
    handleDeleteClick(item) {
      if (this.resumeDataList && this.resumeDataList.length === 1) {
        this.$message.warning("至少要保留一条教育背景");
        return;
      }
      //console.log("点击了删除");
      this.$confirm("确定删除该条教育背景吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //下面才是删除方法
          const finalParams = {
            op: "cma_weixin_deal",
            func: "updataObjColumn",
            // version: "7.3.5",
            forName: "iphone",
            param: {
              person_id: this.personId,
              conditionArr: {
                deledus: item.edusId,
              },
            },
          };
          //console.log("handleDeleteClick-----------finalParams:", finalParams);
          this.resumeGeneralInterface(
            finalParams,
            () => {
              this.handleCancelClick(item);
              this.$message.success("操作成功");
              this.$emit("reqGetIntenedInfo");
            },
            () => {}
          );
        })
        .catch(() => {});
    },
    handleCancelClick(item) {
      this.changeEditStatus(item, false);
      if (item.isNew) {
        this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1); // 新的删除
      } else {
        //初始化赋值
        this.$set(item, "editStatus", false);
        this.$set(item, "editForm", { ...item });
        item.editForm.zyeArr = [item.zym, item.zye];
        item.editForm.startdate = [item.startdate, item.stopdate];
      }
    },
    handleEduId(val) {
      let status = eduList.find((ele) => +ele.value === +val);
      return status ? status.label : "未设置";
    },
    handleEduChange(item) {
      // 找到选中项的对象
      const selectedOption = this.eduList.find(
        (eduList) => eduList.value === item.editForm.eduId
      );
      if (selectedOption) {
        item.editForm.eduId_xueli = selectedOption.label;
        //console.log("-----Value:", selectedOption.value);
        //console.log("------Label:", selectedOption.label);
      } else {
        //console.log("-------没有找到匹配的选项");
      }
      //console.log("----------->handleEduChange", item);
    },
    async handleSave(item) {
      this.$refs["ruleForm" + item.editForm.edusId][0].validate((valid) => {
        if (valid) {
          const finalParams = {
            op: "cma_weixin_deal",
            func: "updataObjColumn",
            // version: "7.3.5",
            forName: "iphone",
            param: {
              person_id: this.personId,
              conditionArr: {
                edus: {
                  school: item.editForm.school,
                  eduId_xueli: item.editForm.eduId_xueli,
                  eduId: item.editForm.eduId,
                  startdate: item.editForm.startdate[0],
                  stopdate: item.editForm.startdate[1],
                  edus: item.editForm.edus,
                  zye: item.editForm.zyeArr[1],
                  zym: item.editForm.zyeArr[0],
                  edusId: item.editForm.edusId,
                },
              },
            },
          };
          //console.log("handleSave-----------finalParams:", finalParams);
          this.resumeGeneralInterface(
            finalParams,
            () => {
              this.handleCancelClick(item);
              this.$message.success("操作成功");
              this.$emit("reqGetIntenedInfo");
            },
            () => {}
          );
        } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.resumeItemContainer {
  display: flex;
  flex-direction: column;
  margin: 20px 0 16px;
  .resumeItemTitle {
    display: flex;
    align-items: center;
    .resumeItemTitleFlag {
      width: 4px;
      height: 19px;
      background: #0b6eff;
      border-radius: 2px;
      margin-right: 10px;
    }
    .resumeItemTitleContent {
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 20px;
      color: #333333;
      margin-right: 19px;
    }
    .resumeItemTitleAddIcon {
      display: flex;
      align-items: center;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #4880ff;
      cursor: pointer;
      .resumeItemAddIcon {
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
    }
  }
  .resumeItemMain {
    margin-top: 16px;
    .resumeItemDisplayList {
      .resumeItemDisplayItem {
        position: relative;
        margin-bottom: 16px;
        &:last-of-type {
          margin-bottom: 0px;
        }
        &:first-of-type {
          .displayBlock {
            .summaryContainer {
              .summary {
                // margin-top: 0px;
              }
            }
          }
        }
        &:hover {
          .displayBlock {
            background: #f8f8f8;
            border-radius: 8px;
            padding: 16px 16px 16px 16px;
          }
          .resumeItemDisplayItemEditButtons {
            display: block;
          }
        }
        .resumeItemDisplayItemEditButtons {
          display: none;
          position: absolute;
          top: -18px;
          right: 0;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          color: #4880ff;
          line-height: 18px;
          & > span {
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            margin-left: 23px;
            user-select: none;
            & > img {
              width: 16px;
              height: 16px;
              margin-right: 5px;
            }
          }
        }
        .displayBlock {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          // margin-right: 150px;
          color: #666666;
          line-height: 18px;
          text-align: left;
          font-style: normal;
          .displayInfo {
            display: flex;
            align-items: center;
          }
          .summaryContainer {
            position: relative;
            .summary {
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 14px;
              color: #888888;
              line-height: 21px;
              text-align: left;
              font-style: normal;
              margin-top: 9px;
              margin-bottom: 0px;
            }
          }
          .bold {
            font-weight: bold;
            font-size: 15px;
            color: #000000;
            line-height: 21px;
          }
          .nobold {
            font-size: 15px;
            color: #000000;
            line-height: 21px;
          }
          .divide {
            width: 1px;
            height: 11px;
            background: #cccfd7;
            margin: 0 12px;
          }
        }
        .editBlock {
          margin-top: 2px;
          background: #f8f8f8;
          border-radius: 8px;
          padding: 16px;
          ::v-deep .el-form-item {
            display: inline-flex;
            flex-direction: column;
            min-width: 251px;
            width: calc(33% - 40px);
            margin-right: 24px;
            margin-bottom: 16px;
            margin-top: 0px;
            &.textAreaFormItem {
              width: 100%;
              margin-bottom: 14px;
              .el-textarea {
                textarea {
                }
              }
            }
            &.wholeLineFormItem {
              width: 100%;
              margin-bottom: 0px;
              .el-form-item__content {
                display: flex;
                justify-content: space-between;
                .submitButtons {
                  width: 100%;
                  display: flex;
                  justify-content: flex-end;
                  margin-top: 11px;
                  .el-button {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 100px;
                    height: 30px;
                    background: #ffffff;
                    border-radius: 4px;
                    border: 1px solid #cdcdcd;
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 14px;
                    text-align: left;
                    font-style: normal;
                    padding: 0;
                    &.el-button--primary {
                      width: 100px;
                      height: 30px;
                      background: #4880ff;
                      border-radius: 4px;
                    }
                  }
                }
                &::before {
                  display: none;
                }
                &::after {
                  display: none;
                }
              }
            }
            .el-form-item__label {
              height: 22px;
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 14px;
              color: #666660;
              line-height: 20px;
              text-align: left;
              font-style: normal;
            }
          }
          .submitButtons {
            display: flex;
            justify-content: flex-end;
            .el-button {
              display: flex;
              justify-content: center;
              align-items: center;
              width: 100px;
              height: 30px;
              background: #ffffff;
              border-radius: 4px;
              border: 1px solid #cdcdcd;
              font-family: PingFang-SC, PingFang-SC;
              font-weight: bold;
              font-size: 14px;
              text-align: left;
              font-style: normal;
              padding: 0;
              &.el-button--primary {
                width: 100px;
                height: 30px;
                background: #4880ff;
                border-radius: 4px;
              }
            }
          }
        }
      }
    }
  }
  // ::v-deep .el-date-editor .el-range-input {
  //   background-color: #ffffff;
  // }
  ::v-deep .el-textarea {
    margin-top: 4px;
  }
  ::v-deep .el-select {
    .el-input__inner {
      width: 250px;
    }
  }
}
::v-deep .el-textarea .el-input__count {
  background-color: transparent;
  bottom: -33px;
}
</style>
